<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;
use app\libs\Predis;
use app\model\Chain;


class ParkModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'parks';
    
    // 设置字段信息
    protected $schema111 = [
        'id'          => 'int',
        'park_name'        => 'string',
    ];
    
    // 设置当前模型的数据库连接
    protected $connection ='db_yunduan';
   
   /**
    * 园区列表
    */
    public function GetList($field='*',$condition=array(),$page=1,$limit=15){

        $obj = new ParkModel();
        $obj=$obj->alias('a');
        $where="a.status=1 ";
        /***关键字*/
        
    	if(isset($condition['keywords'])&&!empty($condition['keywords'])){
    		$where.=" and a.park_name like '%{$condition['keywords']}%' ";
    	}
       /***行业*/
       if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
            $obj=$obj->join('parks_industry b','a.id=b.park_id','left');
            $where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
       }
    	
       /***云端产业链*/
    	if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
    		$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
    		$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
    	}
       /***地方产业链*/
        if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){ 
        	$ChainModel=new Chain();
        	$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
            $obj=$obj->join('parks_chain c','a.id=c.park_id','left');
            $where.=" and c.status=1 and c.chain_id=$chain_id ";
        }else{
        	//全部产业链
        	if(isset($condition['user_id'])&&!empty($condition['user_id'])){
        		$ChainModel=new Chain();
        		$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
        		if(!empty($chain_ids)){
        			$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
        			$chain_ids=implode(',',$chain_ids);
        			$where.=" and c.chain_id in ($chain_ids) ";
        		}
        	}
        }
    	
       /***城市*/
       if(isset($condition['city_id'])&&!empty($condition['city_id'])){
          $where.=" and (a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} or a.country_id={$condition['city_id']} ) ";
        }   
        
        $listobj=$obj->field($field)->where($where)->order('a.sort','desc')->order('a.id','asc')->paginate($limit)->toArray();

        return $listobj;        
    }
    /**
     * 获取园区数量
     */
    public function GetTotal($condition=array()){  	
    	$obj = new ParkModel();
    	$obj=$obj->alias('a');
    	$where="a.status=1 ";
    	/***关键字*/
    	
    	if(isset($condition['keywords'])&&!empty($condition['keywords'])){
    		$where.=" and a.park_name like '%{$condition['keywords']}%' ";
    	}
    	/***行业*/
    	if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
    		$obj=$obj->join('parks_industry b','a.id=b.park_id','left');
    		$where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
    	}
    	
    	/***云端产业链*/
    	if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
    		$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
    		$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
    	}
    	/***地方产业链*/
    	if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
    		$ChainModel=new Chain();
    		$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
    		
    		$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
    		$where.=" and c.status=1 and c.chain_id=$chain_id ";
    	}else{
    		//全部产业链
    		if(isset($condition['user_id'])&&!empty($condition['user_id'])){
    			$ChainModel=new Chain();
    			$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
    			if(!empty($chain_ids)){
    				$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
    				$chain_ids=implode(',',$chain_ids);
    				$where.=" and c.chain_id in ($chain_ids) ";
    			}
    		}
    	}
    	
    	/***城市*/
    	if(isset($condition['city_id'])&&!empty($condition['city_id'])){
    		$where.=" and (a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} ) ";
    	}
    	
    	$total=$obj->where($where)->count();
    	
    	return $total;
    }


}
 